<template>
<view>
<view class="container">
    <view class="nav">
        <navigator :class="'nav-item  ' + (requestUrl == requestData.orderby_default ? 'red-on' : '')" @tap="changeTab" :data-href="requestData.orderby_default">综合<view class="ico-dg"><image class="wh100" src="/static/images/dg.png"></image></view></navigator>
        <navigator :class="'nav-item  ' + (requestUrl == requestData.orderby_sales_sum ? 'red-on' : '')" @tap="changeTab" :data-href="requestData.orderby_sales_sum">销量</navigator>
        <navigator :class="'nav-item  ' + (requestData.sort!='shop_price' ? '' : 'red-on')" @tap="changeTab" :data-href="requestData.orderby_price">
            价格
            <view :class="'ico-dir ' + (requestData.sort!='shop_price'?'':(requestData.sort_asc=='asc'?'ico-dir-up':'ico-dir-dn'))" :style="'background-image: url(' + (resourceUrl+'/static/images/sxjt.png') + ');'"></view>
        </navigator>
        <navigator class="nav-item" @tap="changeTab">筛选<view class="ico-filter"><image class="wh100" src="/static/images/xx.png"></image></view></navigator>
        <navigator class="nav-item search" @tap.stop="listShow" :hidden="listType"><image class="wh100 search-img" src="/static/images/goodslistshow2.png"></image></navigator>
         <navigator class="nav-item search" @tap.stop="listShow" :hidden="!listType"><image class="wh100 search-img" src="/static/images/goodslistshow1.png"></image></navigator>
    </view>

    <view class="choice_list" :hidden="!listType">
        <block v-for="(item, index) in requestData.goods_list" :key="index">
            <navigator class="choice_item" :url="'/pages/goods/goodsInfo/goodsInfo?goods_id=' + item.goods_id">
                <view class="img-wrap">
                <view class="h-label" v-if="item.label_name" style="background: url(/images/h-label.png) no-repeat;background-size:90rpx 25rpx;">{{item.label_name}}</view>
                    <image :src="url + '/api/goods/goodsThumImages?goods_id=' + item.goods_id + '&width=400&height=400'"></image>
                </view>
                <view class="item-cont">
                    <view class="title">{{item.goods_name}}</view>
                    <view class="containers">
                    <view class="price">￥{{item.shop_price}}</view>
                    <view class="carts" v-if="is_on" @tap.stop="openSpecModel" :data-goods_id="item.goods_id">
                      <image src="/static/images/shopc.png" style="width:45rpx;height:45rpx;"></image>
                    </view>                  
                   </view>
                   
                    <!-- <view class="comment">评论{{item.comment_count  + item.virtual_comment_count}} 销量{{item.sales_sum  + item.virtual_sales_sum}}</view> -->
                </view>
            </navigator>
        </block>
    </view>


 <view class="choice_box" :hidden="listType">
        <view class="choice_list">
            <block v-for="(item, index) in requestData.goods_list" :key="index">
                <view class="choice_item">
                    <navigator :url="'/pages/goods/goodsInfo/goodsInfo?goods_id=' + item.goods_id">
                        <view class="h-label" v-if="item.label_name" style="background: url(/images/h-label.png) no-repeat;background-size:175rpx 40rpx;">{{item.label_name}}</view>
                        <image :src="url + '/api/goods/goodsThumImages?goods_id=' + item.goods_id + '&width=400&height=400'"></image></navigator>
                    <navigator class="title ellipsis-2" :url="'/pages/goods/goodsInfo/goodsInfo?goods_id=' + item.goods_id">{{item.goods_name}}</navigator>
                    <view class="choice_footer">
                        <view class="price">￥{{item.shop_price}}</view>
                         <!-- <view class="similer">评论{{item.comment_count  + item.virtual_comment_count}} 销量{{item.sales_sum  + item.virtual_sales_sum}}</view> -->
                    </view>
                </view>
            </block>
        </view>
    </view>


    
    <view class="no-data" v-if="!requestData.goods_list || requestData.goods_list.length == 0">
        <image src="/static/images/cart-null.png" class="cart-image"></image>
        <view class="no-data-title">没有相关的数据</view>
        <navigator url="/pages/index/index/index" class="lookat"> 去逛逛 </navigator>
    </view>
</view>

<!-- 筛选弹框  -->
<view :hidden="!openFilterModal">
    <view class="cover-layer" @tap="closeFilterModal"></view>
    <view class="filter-modal">
        <icon type="cancel" color="gray" size="22" class="modal-close" @tap="closeFilterModal"></icon>
        <button class="viewall-btn" @tap="restoreData">显示全部分类</button>
         <view class="filter-box" v-if="requestData.filter_menu.length >0">当前选中</view>
         <view v-for="(item, index) in requestData.filter_menu" :key="index" class="filter-items" @tap.stop="filterGoods" :data-href="item.href"><view class="filter-item" style="background-color:#ff6a00;color:#fff;margin:10rpx">{{item.name}}</view></view>
        <view v-for="(item, index) in requestData.filter_spec" :key="index" class="filter-box">
            <view class="filter-name">{{item.name}}</view>
            <view class="filter-items">
                <view v-for="(items, index2) in item.item" :key="index2" class="filter-item" :data-href="items.href" @tap="filterGoods">
                {{items.name}}</view>
            </view>
        </view>
        <view v-for="(item, index) in requestData.filter_attr" :key="index" class="filter-box">
            <view class="filter-name">{{item.name}}</view>
            <view class="filter-items">
                <view v-for="(items, index2) in item.item" :key="index2" class="filter-item" :data-href="items.href" @tap="filterGoods">
                {{items.name}}</view>
            </view>
        </view>
        <view class="filter-box" v-if="requestData.filter_brand.length>0">
            <view class="filter-name">相关品牌</view>
            <view class="filter-items">
                <view v-for="(items, index) in requestData.filter_brand" :key="index" class="filter-item" :data-href="items.href" @tap="filterGoods">
                {{items.name}}</view>
            </view>
        </view>
        <view class="filter-box" v-if="requestData.filter_price.length>0">
            <view class="filter-name">价格区间</view>
            <view class="filter-items">
                <view v-for="(items, index) in requestData.filter_price" :key="index" class="filter-item" :data-href="items.href" @tap="filterGoods">
                {{items.name}}</view>
            </view>
        </view>
    </view>
</view>




<!-- 规格弹窗 -->
<view :hidden="!openSpecModal">
    <view class="cover-layer" @tap="closeSpecModal"></view>
    <view class="spec-model">
        <icon type="cancel" color="gray" size="22" class="modal-close" @tap="closeSpecModal"></icon>
        <view class="spec-goods">
            <image class="wh100 spec-img" :src="url + '/api/goods/goodsThumImages?goods_id=' + data.goods.goods_id + '&width=200&height=200'"></image>
            <view class="spec-goods-info">
                <view class="spec-goods-name">{{data.goods.goods_name}}</view>
                  <view class="prom-info" v-if="select.activity.prom_type && select.activity.prom_type != 6">
                    <text class="prom-type">{{select.activity.countName}}</text>
                    <text>{{select.activity.countTime}}</text>
                </view>
                 <navigator class="prom-info" v-if="select.activity.prom_type == 6" :url="'../../team/team_info/team_info?goods_id=' + select.activity.goods_id + '&team_id=' + select.activity.team_id + '&item_id=' + select.activity.item_id">
                    <text class="prom-type">{{select.activity.countName}}</text>
                    <view style="display:inline-block;">点击前往</view>
                </navigator>
                <view class="spec-goods-price">{{select.activity.priceName?select.activity.priceName:''}}￥ {{select.price[0]}}.{{select.price[1]}}
                </view>
                <view class="spec-goods-stock">剩余库存：{{select.stock}}</view>
            </view>
        </view>
        <block v-for="(item, listIdx) in data.goods_spec_list" :key="listIdx">
            <view class="spec-name">规格</view>
            <view class="spec_types">
            <view v-for="(spec, index) in item.spec_item" :key="index" :class="'spec-btn ' + (item.selectItemId==spec.id?'spec-btn-click':'')" @tap="selectSpec" :data-listidx="listIdx" :data-itemid="spec.id">{{spec.item}}</view>
            </view>
        </block>
        <view class="spec-box">
          <view class="spec-name">数量</view>
          <view class="count">
              <view class="sub" @tap="subCartNum">-</view>
              <input type="number" :value="goodsInputNum" @blur="inputCartNum"></input>
              <view class="add" @tap="addCartNum">+</view>
          </view>
        </view>
        <view class="spec-cart-btns">
                <view v-if="data.goods.is_virtual" :class="'spec-cart-btn spec-buy spec-cart-btn-lg ' + (select.stock<=0||shippingCost<0||!hasSpec?'spec-cart-disable':'')" data-action="buy" @tap="addCart">立即购买</view>
            <block v-else-if="select.activity.prom_type == 8">                
                        <view :class="'spec-cart-btn spec-buy spec-cart-btn-lg ' + (select.stock<=0||shippingCost<0||!hasSpec?'spec-cart-disable':'')" @tap="bargainBuy" data-action="add" :data-status="isSeparate">{{isSeparate?'砍价拿':'加入购物车'}}</view>                   
            </block>
                 <view v-else-if="select.activity.prom_type == 4" :class="'spec-cart-btn spec-buy spec-cart-btn-lg ' + (select.stock<=0||shippingCost<0||!hasSpec?'spec-cart-disable':'')" data-action="reserve" @tap="addCart">立即预订</view>
           
            <block v-else-if="data.goods.exchange_integral <= 0">
                <!-- <view class="spec-cart-btn spec-add-cart {{select.stock<=0||shippingCost<0||!hasSpec?'spec-cart-disable':''}}" data-action='add' bindtap="addCart">加入购物车</view> -->
                <view :class="'spec-cart-btn spec-buy ' + (select.stock<=0||shippingCost<0||!hasSpec?'spec-cart-disable':'')" data-action="add" @tap="addCart">确定</view>
            </block>
            <view v-else :class="'spec-cart-btn spec-buy spec-cart-btn-lg ' + (select.stock<=0||shippingCost<0||!hasSpec?'spec-cart-disable':'')" data-action="exchange" @tap="addCart">立即兑换</view>
        </view>
    </view>
</view>
</view>
</template>

<script>
var app = getApp();
import LoadMore from "../../../utils/LoadMore";
var util = require("../../../utils/util.js");
var load = new LoadMore();
var request = app.globalData.request;

export default {
  data() {
    return {
      url: app.globalData.setting.url,
      resourceUrl: app.globalData.setting.resourceUrl,
      currentPage: 1,
      requestData: null,
      //请求的数据
      allData: null,
      //第一次请求到的所有数据，用于恢复筛选数据
      openFilterModal: false,
      //打开筛选弹框
      baseUrl: '/api/goods/goodsList',
      //基地址
      requestUrl: '',
      //请求的链接
      openSpecModal: false,
      //是否打开规格弹窗
      goodsInputNum: 1,
      //选中的商品件数
      specSelect: 0,
      //选中的组合规格数组spec_goods_price下标
      optionItemId: 0,
      //页面参数，页面初始化指定显示的itemid，用于活动
      shippingCost: 0,
      hasSpec: true,
      select: {
        //选择的(规格)商品的参数，用于显示
        price: 0,
        stock: 0,
        specName: '',
        activity: null
      },
      is_on: true,
      // 是否打开快速下单入口
      isBragain: false,
      isSeparate: true,
      listType: false // 列表展示类型 
      ,
      enterAddressPage: false,
      data: "",
      cartGoodsNum: "",
      selecteds: ""
    };
  },

  components: {},
  props: {},
  onLoad: function (options) {
    load.init(this, 'goods_list', 'requestData');
    var requestUrl = this.baseUrl + (typeof options.cat_id != 'undefined' ? '?id=' + options.cat_id : '');
    this.requestGoodsList(requestUrl);
  },
  onReachBottom: function () {
    if (load.canloadMore()) {
      this.requestGoodsList(this.requestUrl);
    }
  },
  onPullDownRefresh: function () {
    this.resetData();
    this.requestGoodsList(this.requestUrl);
  },
  methods: {
    listShow: function () {
      if (!this.listType) {
        this.setData({
          listType: true
        });
      } else {
        this.setData({
          listType: false
        });
      }
    },
    changeTab: function (e) {
      this.resetData();
      this.requestGoodsList(e.currentTarget.dataset.href);
    },
    requestGoodsList: function (requestUrl) {
      var that = this;
      this.requestUrl = requestUrl;
      this.setData({
        requestUrl: requestUrl
      });
      requestUrl += (requestUrl.indexOf('?') > 0 ? '&' : '?') + 'p=' + that.currentPage;
      load.request(requestUrl, function (res) {
        that.currentPage++;

        if (that.allData == null) {
          that.allData = Object.assign({}, res.data.result);
        }

        uni.stopPullDownRefresh();
      });
    },

    /** 购买虚拟商品 */
    buyVirtualGoods: function (data) {
      //检查用户是否登录方可操作立即购买
      if (!app.globalData.auth.isAuth()) {
        app.globalData.showLoading(null, 1500);
        app.globalData.getUserInfo();
        return;
      }

      Object.assign(data, {
        goods_name: this.data.goods.goods_name,
        spec_name: this.select.specName,
        price: this.select.price
      });
      uni.navigateTo({
        url: '/pages/virtual/buy_step/buy_step?' + util.Obj2Str(data)
      });
    },

    /** 立即兑换 */
    exchange: function (data) {
      //检查用户是否登录方可操作立即购买
      if (!app.globalData.auth.isAuth()) {
        app.globalData.showLoading(null, 1500);
        app.globalData.getUserInfo();
        return;
      }

      if (app.globalData.userInfo.pay_points < this.data.goods.exchange_integral) {
        return app.globalData.showTextWarining('您的积分不够喔~');
      }

      if (this.shippingCost < 0 || this.select.stock <= 0 || !this.hasSpec) {
        return;
      }

      this.setData({
        openSpecModal: false,
        enterAddressPage: true
      });
      uni.navigateTo({
        url: '/pages/cart/integral/integral?' + util.Obj2Str(data)
      });
    },

    /** 立即预订 */
    reserve: function (data) {
      var that = this; //检查用户是否登录方可操作立即购买

      if (!app.globalData.auth.isAuth()) {
        app.globalData.showLoading(null, 1500);
        app.globalData.getUserInfo();
        return;
      }

      if (this.shippingCost < 0 || this.select.stock <= 0 || !this.hasSpec) {
        return;
      }

      Object.assign(data, {
        action: 'reserve',
        prom_id: that.select.activity.prom_id ? that.select.activity.prom_id : ''
      });
      this.setData({
        openSpecModal: false,
        enterAddressPage: true
      });
      uni.navigateTo({
        url: '/pages/cart/reserve/reserve?' + util.Obj2Str(data)
      });
    },

    /** 立即购买 */
    buyNow: function (data) {
      //检查用户是否登录方可操作立即购买
      if (!app.globalData.auth.isAuth()) {
        app.globalData.showLoading(null, 1500);
        app.globalData.getUserInfo();
        return;
      }

      if (this.shippingCost < 0 || this.select.stock <= 0 || !this.hasSpec) {
        return;
      }

      Object.assign(data, {
        action: 'buy_now'
      });
      this.setData({
        openSpecModal: false
      });
      uni.navigateTo({
        url: '/pages/cart/cart2/cart2?' + util.Obj2Str(data)
      });
    },

    /** 关闭规格弹窗 */
    closeSpecModal: function () {
      this.setData({
        openSpecModal: false
      });
    },

    /** 打开规格弹窗 */
    openSpecModel: function (e) {
      var that = this;
      request.get('/api/goods/goodsInfo', {
        data: {
          id: e.currentTarget.dataset.goods_id
        },
        failRollback: true,
        success: function (res) {
          that.setData({
            data: res.data.result,
            openSpecModal: true,
            isSeparate: true
          });
          that.initData(res.data.result);
        }
      });
    },

    /** 初始化数据，注意顺序 */
    initData: function (data) {
      //检查商品
      this.initCheckGoods(data); //检查一下购物的数量，可能无库存

      this.checkCartNum(this.goodsInputNum);
    },

    /** 检查商品 */
    initCheckGoods: function (data) {
      var that = this;
      request.post('/api/goods/activity', {
        data: {
          goods_id: data.goods.goods_id,
          item_id: data.goods.spec_goods_price.length > 0 ? data.goods.spec_goods_price[0]['item_id'] : ''
        },
        success: function (res) {
          //初始化规格
          if (res.data.result.goods.activity_is_on == 1) {
            that.initSpecsPrice(res.data.result);
          } else {
            data.goods.prom_type = 0;
            that.initSpecsPrice(data);
          }
        }
      });
    },

    /** 初始化所有规格 */
    initSpecsPrice: function (data) {
      var specSelect = 0; //初始化选中第一个规格 

      var specs = this.data.goods.spec_goods_price;

      if (specs.length == 0) {
        //没有规格
        this.initActivity(data.goods);
        return;
      } //第一次请求的总数据中的activity默认是第一种规格的,可减少一次请求
      //specs[0].activity = data.goods;


      if (this.optionItemId) {
        //指定规格
        for (var i = 0; i < specs.length; i++) {
          if (specs[i].item_id == this.optionItemId) {
            specSelect = i;
            break;
          }
        }
      } else {
        //初始化选库存不为0的规格
        for (var i = 0; i < specs.length; i++) {
          if (specs[i].store_count <= 0) {
            continue;
          }

          specSelect = i;
          break;
        }
      }

      specs[specSelect].activity = data.goods; //生成子规格组(goods_spec_list)的各自选中项

      var specIds = specs[specSelect].key.split("_");
      var list = this.data.goods.spec; // for (var i = 0; i < list.length; i++) {
      //     for (var j = 0; j < list[i].spec_item.length; j++) {
      //         if (util.inArray(list[i].spec_item[j].id, specIds)) {
      //             list[i].selectItemId = list[i].spec_item[j].id;
      //             break;
      //         }
      //     }
      // }
      // this.setData({
      //     specSelect: specSelect,
      //     'data.goods_spec_list': list,
      //     'data.goods.spec_goods_price': specs
      // });

      this.initSelectSpecGoods();
    },

    /** 初始化选中的规格商品 */
    initSelectSpecGoods: function () {
      var specSelect = this.specSelect;
      var specs = this.data.goods.spec_goods_price;
      var itemId = specs[specSelect].item_id;

      if (specs[specSelect].prom_type == 0) {
        var noActivity = {
          prom_type: 0
        };
        specs[specSelect].activity = noActivity;
        this.initActivity(noActivity);
      } else if (typeof specs[specSelect].activity != 'undefined') {
        this.initActivity(specs[specSelect].activity);
      } else {
        this.requestSpecInfo(specSelect);
      }
    },

    /** 点击规格按钮的回调函数 */
    selectSpec: function (e) {
      //对商品数量进行判断，对库存进行判断
      var itemId = e.currentTarget.dataset.itemid;
      var listIdx = e.currentTarget.dataset.listidx;
      var list = this.data.goods_spec_list;

      if (list[listIdx].selectItemId == itemId) {
        return;
      }

      list[listIdx].selectItemId = itemId;
      var newSpecKeys = [];

      for (var i = 0; i < list.length; i++) {
        newSpecKeys[i] = list[i].selectItemId;
      } //item排序,生成key


      var newSpecKeys = util.sortSize(newSpecKeys).join('_');
      var newSpecSelect = 0;
      var specs = this.data.goods.spec_goods_price;
      var hasSpec = false;

      for (var i = 0; i < specs.length; i++) {
        if (specs[i].key == newSpecKeys) {
          hasSpec = true;
          newSpecSelect = i;
          break;
        }
      }

      this.setData({
        hasSpec: hasSpec,
        specSelect: newSpecSelect,
        'data.goods_spec_list': list,
        isBragain: this.isSeparate ? false : true
      });
      this.initSelectSpecGoods();
      this.checkCartNum(this.goodsInputNum);
    },

    /** 初始化显示的活动信息 */
    initActivity: function (activity) {
      if (activity.prom_type && activity.prom_type != 6) {
        var startTime = new Date().getTime();

        if (activity.prom_type == 1) {
          //抢购
          activity.priceName = '抢购价';
          activity.countName = '限时抢购';
        } else if (activity.prom_type == 2) {
          //团购
          activity.priceName = '团购价';
          activity.countName = '限时团购';
        } else if (activity.prom_type == 3) {
          //促销
          activity.countName = '优惠促销';
        } else if (activity.prom_type == 4) {
          //预售
          activity.priceName = '预售价';
          activity.countName = '预售商品';
        } else if (activity.prom_type == 8) {
          //砍价
          !this.isBragain ? activity.priceName = '砍价享' : activity.priceName = '';
          !this.isBragain ? activity.countName = '砍价活动' : activity.countName = '';
        }

        activity.countTime = '--天--时--分--秒';

        if (!activity.diffTime) {
          activity.diffTime = new Date().getTime() - activity.server_current_time * 1000;
        }
      } else if (activity.prom_type == 6) {
        activity.countName = '该商品正在参与拼团';
        activity.goods_id = activity.goods_id;
        activity.team_id = activity.prom_id ? activity.prom_id : 0;
        activity.item_id = activity.item_id ? activity.item_id : 0;
      }

      this.setData({
        'select.activity': activity
      });
      this.destroyActivityTimer();
      this.createActivityTimer();
      this.initSelectedData();
    },

    /** 初始化选中的（规格）商品的显示参数 */
    initSelectedData: function () {
      var goods = this.data.goods;
      var activity = this.select.activity;
      var specs = this.data.goods.spec_goods_price;
      var specSelect = this.specSelect;
      var stock = 0;
      var price = 0;

      if (activity.prom_type == 1 || activity.prom_type == 2) {
        price = activity.shop_price;
        stock = activity.store_count;
      } else if (activity.prom_type == 3) {
        price = activity.shop_price; // stock = specs.length > 0 ? specs[specSelect].store_count : goods.store_count;

        stock = activity.store_count;
      } else if (activity.prom_type == 4) {
        price = activity.shop_price; //stock = specs.length > 0 ? specs[specSelect].store_count : goods.store_count;

        stock = activity.store_count;
      } else if (activity.prom_type == 8 && !this.isBragain) {
        price = activity.end_price ? activity.end_price : activity.shop_price;
        stock = activity.store_count;
        this.setData({
          'select.bargain_price': price
        });
      } else if (specs.length > 0) {
        price = specs[specSelect].price;
        stock = specs[specSelect].store_count;
      } else {
        price = goods.shop_price;
        stock = goods.store_count;
      }

      price = parseFloat(price).toFixed(2).toString().split('.');
      this.setData({
        'select.price': price,
        'select.stock': stock,
        'select.specName': specs.length > 0 ? specs[specSelect].key_name : ''
      });

      if (this.isBragain) {
        this.setData({
          isBragain: false
        });
      }
    },

    /** 检查是否倒计时是否结束 */
    checkActivityTime: function () {
      var that = this;
      var activity = that.select.activity;
      var remainTime = activity.end_time * 1000 - new Date().getTime() + activity.diffTime;

      if (remainTime > 0) {
        remainTime = util.remainTime(remainTime);
        that.setData({
          'select.activity.countTime': remainTime
        });
      } else {
        that.requestSpecInfo(that.specSelect);
        return;
      }
    },

    /** 创建活动倒计时定时器 */
    createActivityTimer: function () {
      var that = this;
      var activity = that.select.activity;

      if (!activity.prom_type || activity.prom_type == 6 || that.isBragain) {
        return;
      }

      that.timer = setInterval(function () {
        that.checkActivityTime();
      }, 1000);
    },

    /** 销毁活动倒计时定时器 */
    destroyActivityTimer: function () {
      if (this.timer) {
        clearInterval(this.timer);
        this.timer = null;
      }
    },

    /** 请求规格商品的活动信息 */
    requestSpecInfo: function (specSelect) {
      var that = this;
      var specs = that.data.goods.spec_goods_price;
      request.get('/api/goods/activity', {
        data: {
          goods_id: that.data.goods.goods_id,
          item_id: specs.length > 0 ? specs[specSelect].item_id : ''
        },
        success: function (res) {
          if (res.data.result.goods.activity_is_on == 1) {
            that.initActivity(res.data.result.goods);
          }
        }
      });
    },

    /** 减少购买的商品数量 */
    subCartNum: function (e) {
      this.checkCartNum(this.goodsInputNum - 1);
    },

    /** 增加购买的商品数量 */
    addCartNum: function (e) {
      this.checkCartNum(this.goodsInputNum + 1);
    },

    /** 输入购买的数量 */
    inputCartNum: function (e) {
      this.checkCartNum(Number(e.detail.value));
    },

    /** 检查购买的数量 */
    checkCartNum: function (num) {
      var specs = this.data.goods.spec_goods_price;
      var specSelect = this.specSelect;
      var activity = this.select.activity;
      var stock = this.data.goods.store_count; //参与活动

      if (activity && activity.activity_is_on == 1) {
        stock = activity.store_count;
      } else {
        if (specs.length > 0) {
          stock = specs[specSelect].store_count;
        }
      }

      if (num > stock || stock == 0) {
        num = stock;
      } else if (num < 1) {
        num = 1;
      }

      this.setData({
        goodsInputNum: num
      });
    },

    /** 加入购物车 */
    addCart: function (e) {
      var that = this;
      var itemId = 0;
      var specs = this.data.goods.spec_goods_price; //区分有规格和无规格

      if (specs.length > 0) {
        if (specs[this.specSelect].store_count <= 0) {
          return app.globalData.showTextWarining("当前无库存！");
        }

        itemId = specs[this.specSelect].item_id;
      } else {
        if (this.data.goods.store_count <= 0) {
          return app.globalData.showTextWarining("当前无库存！");
        }
      }

      if (this.goodsInputNum <= 0) {
        return app.globalData.showTextWarining("商品数量不能小于1");
      }

      var data = {
        goods_id: this.data.goods.goods_id,
        goods_num: this.goodsInputNum,
        item_id: itemId
      };

      if (this.data.goods.is_virtual) {
        return this.buyVirtualGoods(data);
      }

      if (e.currentTarget.dataset.action == 'add') {
        //加入购物车
        if (this.shippingCost < 0 || this.select.stock <= 0 || !this.hasSpec) {
          return;
        }

        request.post('/api/cart/add', {
          data: data,
          success: function (res) {
            that.setData({
              openSpecModal: false
            });
            request.checkUniqueId();
            app.globalData.showSuccess('添加成功');
            that.setData({
              cartGoodsNum: res.data.result
            });
          }
        });
      } else if (e.currentTarget.dataset.action == 'exchange') {
        //立即兑换
        this.exchange(data);
      } else if (e.currentTarget.dataset.action == 'reserve') {
        //立即预订
        this.reserve(data);
      } else {
        //立即购买
        this.buyNow(data);
      }
    },
    openFilterModalFun: function () {
      this.setData({
        openFilterModal: true
      });
    },
    closeFilterModal: function () {
      this.setData({
        openFilterModal: false
      });
    },

    /** 商品筛选 */
    filterGoods: function (e) {
      this.resetData();
      this.requestGoodsList(e.currentTarget.dataset.href);
      this.setData({
        selecteds: this.selecteds
      }); // this.closeFilterModal();
    },

    /** 重置数据 */
    resetData: function () {
      load.resetConfig();
      this.requestData = null;
      this.currentPage = 1;
    },
    restoreData: function () {
      this.setData({
        'requestData': this.allData,
        selecteds: []
      });
    },

    /** 砍价购买 */
    bargainBuy: function (e) {
      var that = this;

      if (!app.globalData.auth.isAuth()) {
        app.globalData.showLoading(null, 1500);
        app.globalData.getUserInfo();
        return;
      }

      var data = e.currentTarget.dataset;

      if (data.status) {
        //砍价拿
        that.setData({
          openSpecModal: false
        });
        var specs = that.data.goods.spec_goods_price;
        var specSelect = that.specSelect;
        var item_id = specs.length > 0 ? specs[specSelect]['item_id'] : '';

        if (that.select.stock == 0 || Number(that.select.stock) - Number(that.goodsInputNum) < 0) {
          that.setData({
            goodsInputNum: 1
          });
          return app.globalData.showTextWarining('砍价库存不足!');
        }

        uni.showModal({
          content: '确定发起砍价吗?',
          success: function (res) {
            if (res.confirm) {
              //去下单，并且发起砍价活动
              request.post('/api/Bargain/start_bargain', {
                data: {
                  bargain_id: that.select.activity.prom_id,
                  goods_num: that.goodsInputNum,
                  item_id: item_id
                },
                success: function (res) {
                  if (1 == res.data.status) {
                    uni.navigateTo({
                      url: '/pages/Bargain/bargain/bargain?id=' + res.data.result.id
                    });
                  } else {
                    app.globalData.showTextWarining(res.data.msg);
                  }
                }
              });
            }
          }
        });
      } else {
        //单独购
        that.addCart(e);
      }
    }
  }
};
</script>
<style>
/* goodsList.wxss */
.container {
    color: #666;
}
.carts{
    position: absolute;
    right:30rpx;
    bottom:30rpx;
    width:45rpx;
    display:inline-block;
    height:45rpx;
}
.containers {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
}
.nav-item{
    float: left;
    width: 22.5%;
    height: 90rpx;
    line-height: 90rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 28rpx;
    border-bottom: 1px solid #f3f3f3;
    background-color: #fff;
}
.nav-item .ico-dg{
    width: 18rpx;
    height: 12rpx;
    margin-left: 10rpx;
    line-height: 0;
}
.nav-item .ico-filter{
    width: 20rpx;
    height: 20rpx;
    margin-left: 10rpx;
    line-height: 0;
}
.ico-dir{
    width: 16rpx;
    height: 22rpx;
    margin-left: 10rpx;  
    background-repeat: no-repeat;
    background-size: cover;
    background-position-x: 0;
}
.ico-dir-dn {
    background-position-x: -16rpx;
}
.ico-dir-up {
    background-position-x: -32rpx;
}
.search{
    width: 10%
}
.search-img{
    width: 30rpx;
    height: 30rpx;
}
.choice_list{
    background-color: #fff;
}
.choice_item{
    position: relative;
    min-height: 200rpx;
    padding: 10rpx 0;
    border-bottom: 1px solid #f3f3f3;
    font-size: 28rpx;
    width: 710rpx;
    box-sizing: border-box;
    border-radius: 10rpx;
    overflow: hidden;
}
.img-wrap{
    float: left;
    width: 180rpx;
    height: 180rpx;
    padding:0 10rpx 10rpx;
    position: relative;
}
.img-wrap image{
    width: 100%;
    height: 100%;
}
.item-cont .title{
    height: 72rpx;
    margin: 10rpx 0;
    padding-right: 10rpx;
    line-height: 36rpx;
    color: #333;
    overflow: hidden;   
}
.price{
    display: inline-block;
    color: #ff6a00;
    padding-bottom: 20rpx;
}
.comment{
    font-size: 25rpx;
}
/* 弹框相关 */
.filter-modal{
    position: fixed;
    left: 150rpx;
    top: 0;
    right: 0;
    bottom: 0;
    z-index: 99999;
    background-color: white;
    overflow-x: hidden;
    padding-bottom: 30rpx;
}
.filter-box{
    box-sizing: border-box;
    width: 100%;
    font-size: 28rpx;
    padding: 20rpx;
}
.filter-name{
    width: 100%;
    padding: 20rpx 0;
    word-break: keep-all;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    font-size: 30rpx;
}
.filter-items{
    width: 100%;
}
.filter-item{
    float: left;
    width: fit-content;
    max-width: 500rpx;
    word-break: keep-all;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    padding: 10rpx;
    border-radius: 10rpx;
    border: 1rpx  solid #f3f3f3;
    margin: 0 10rpx 10rpx 0;
    background-color: #fdfdfd;
    color: #666;
}
.viewall-btn{
    margin-top: 20rpx;
    width: 300rpx;
}






/* 规格弹框 */
.spec-model{
    position: fixed;
    bottom: 0;
    background: white;
    width: 100%;
    padding: 0 30rpx;
    font-size: 32rpx;
    box-sizing: border-box;
    overflow-x: hidden;
    z-index:9999;
}

.spec-goods{
    padding: 15rpx 0 30rpx;
    float: left;
    width: 100%;
    border-bottom: 1px solid #f3f3f3;
}

.spec-goods-info{
    float: left;
    padding: 0 25rpx; 
    width: 490rpx;
}
.spec-goods-name{
    font-size: 30rpx;
    line-height: 35rpx;
    height: 70rpx;
    margin: 15rpx 20rpx 25rpx 0;
    overflow: hidden;
    text-overflow: ellipsis;
}
.spec-goods-price{
    color: #ff6a00;
    font-size: 30rpx;
    font-weight: bold;
}
.spec-goods-stock{
    margin-top: 10rpx;
    font-size: 26rpx;
    color: #999999;
}
.spec-img{
    float: left;
    height: 180rpx;
    width: 180rpx;
    margin-top:16rpx;
}
.spec-goods-info{
    float: left;
    padding: 0 25rpx; 
    width: 430rpx;
}
.spec-goods-name{
    font-size: 30rpx;
    line-height: 35rpx;
    height: 70rpx;
    margin: 15rpx 20rpx 25rpx 0;
    overflow: hidden;
    text-overflow: ellipsis;
}
.spec-goods-price{
    color: #ff6a00;
    font-size: 30rpx;
    font-weight: bold;
}
.spec-goods-stock{
    margin-top: 10rpx;
    font-size: 26rpx;
    color: #999999;
}
.spec-box{
  width: 100%;
  height: 50rpx;
  overflow: hidden;
  margin-top: 50rpx;
}
.spec-name{
    float: left;
    width: 150rpx;
    font-size: 32rpx;
}
.count{
    float: right;
    width: 184rpx;
    height: 50rpx;
    font-size: 32rpx;
    font-weight: bold;
}
.count>view,.count>input{
    float: left;
    width: 60rpx;
    height: 50rpx;
    line-height: 50rpx;
    text-align: center;
}
.spec-btn{
    color: black;
    background-color: #efefef;
    font-size: 24rpx;
    line-height: 48rpx;
    float: left;
    height: 48rpx;
    padding: 0 24rpx;
    border: 1rpx solid #f3f3f3;
    margin: 4rpx 20rpx 4rpx 0;
    border-radius: 24rpx;
    overflow: hidden;
}
.spec-btn-click{
    color: #ff3a3a;
    background-color: #ffefef;
    border: 1rpx solid #ff6a00;
}
.spec_types{
  width: 100%;
  height: auto;
  overflow: hidden;
  padding-top: 20rpx;
}
.spec-cart-btns{
    width: 100%;
    line-height: 68rpx;
    margin: 54rpx auto 32rpx;
    height: 68rpx;
}
.spec-cart-btn{
    float: left;
    width: 690rpx;
    height: 68rpx;
    font-size: 30rpx;
    text-align: center;
    color: white;
    border-radius: 34rpx;
}
.spec-buy{
    background-color: #ff6a00;
}
.spec-cart-btn-lg{
    width: 614rpx;
}
.spec-cart-disable{
    background-color: #ff6a00;
}
.spec-add-cart{
  display: none;
    background-color: #ffb03f;
}
.cover-layer{
    z-index:999;
}

.prom-info{
    margin: 10rpx 0;
    color: #ff6a00;
}
.prom-info>.prom-type{
    color: white;
    background-color: #ff6a00;
    margin-right: 14rpx;
    padding: 2rpx 8rpx;
    border-radius: 4rpx;
}
.h-label{
  position: absolute;
  font-size: 16rpx;
  color: #ffffff;
  width: 90rpx;
  height: 25rpx;
  line-height: 25rpx;
  padding-left: 8rpx;
}


.choice_box {
    font-size: 12px;
}
.choice_box .choice_list .choice_item{
    width: 368rpx;
    float: left;
    margin: 2rpx;
    background-color: #fff;
}
.choice_box .choice_list image {
    width: 100%;
    height: 368rpx;
}
.choice_box .choice_list .title {
    height: 72rpx;
    line-height: 36rpx;
    font-weight: bold;
    text-align: center;
    color: #333;
}
.choice_box .choice_list .choice_footer {
    display: block; 
    margin: 20rpx 5rpx 15rpx;
    position: relative;
    height: 30rpx;
    line-height: 17rpx;
}
.choice_box .choice_list .choice_footer .price {
  color: #ff6a00;
  font-size:.855rem;
}
.choice_box .choice_list .choice_footer .similer {
    position: absolute;
    bottom: 0;
    right: 0;
    text-align: center;
    color: #686868;
    width: fit-content;
    line-height: 30rpx;
    padding: 1rpx 6rpx;
    border-radius: 5rpx;
     font-size:.655rem;
}



</style>